package com.example.sunmiv1.base;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;

import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sunmiv1.scan.InfraredScanActivity;
import com.example.sunmiv1.utils.zxing.Constant;
import com.example.sunmiv1.utils.zxing.activity.CaptureActivity;
import com.example.sunmiv1.utils.zxing.activity.ScanGoodActivity;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;

public abstract class BaseActivity<T> extends FragmentActivity {

    protected Context context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        context = this;
        initView();
        initData();
        initListener();
    }

    // 初始化頁面
    protected abstract void initView();

    //初始化數據
    protected abstract void initData();

    //初始化組件監聽器（可選）
    protected abstract void initListener();


    // 調起掃碼
    public void startQrCode() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            // 申請權限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, Constant.REQ_PERM_CAMERA);
            return;
        }
        // 二維碼掃碼
       /* Intent intent = new Intent(this, CaptureActivity.class);
        intent.putExtra("scannerType", 0);
        startActivityForResult(intent, Constant.REQ_QR_CODE);*/

        Intent intent = new Intent(context, InfraredScanActivity.class);
        intent.putExtra("scannerType", 0);
        startActivityForResult(intent, Constant.REQ_QR_CODE);

    }

    // 調起掃碼
    private int scannerType = 0;
    List<T> listT;
    public void scanGood(int type, List<T> t) {
        scannerType = type;
        listT = t;
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            // 申請權限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 11);
            return;
        }
        // 二維碼掃碼
        Intent intent = new Intent(this, ScanGoodActivity.class);
        intent.putExtra("scannerType", type);
        intent.putExtra("list", (Serializable) t);
        startActivityForResult(intent, 110);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 11:
                // 攝像頭權限申請
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 獲得授權
                    Intent intent = new Intent(this, CaptureActivity.class);
                    intent.putExtra("scannerType", scannerType);
                    intent.putExtra("list", (Serializable) listT);
                    startActivityForResult(intent, 110);
                } else {
                    // 被禁止授權
                    Toast.makeText(this, "請至權限中心打開本應用的相機訪問權限", Toast.LENGTH_LONG).show();
                }
                break;
            case Constant.REQ_PERM_CAMERA:
                // 攝像頭權限申請
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 獲得授權
                    Intent intent = new Intent(this, CaptureActivity.class);
                    startActivityForResult(intent, Constant.REQ_QR_CODE);
                } else {
                    // 被禁止授權
                    Toast.makeText(this, "請至權限中心打開本應用的相機訪問權限", Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

    /**
     * 點擊某個textView，調起時間選擇器，設置該時間選擇器返回的時間給該textView
     *
     * @param tv textView
     */
    public void getDateFromDatePickerDialog(TextView tv) {
        Calendar calendar = Calendar.getInstance();
        new DatePickerDialog(this, AlertDialog.THEME_HOLO_LIGHT, new DatePickerDialog.OnDateSetListener() {
            // 綁定監聽器(How the parent is notified that the date is set.)
            @SuppressLint("SetTextI18n")
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                // 此處得到選擇的時間，可以進行你想要的操作
                if ((monthOfYear + 1) < 10) {
                    tv.setText(year + "-0" + (monthOfYear + 1) + "-" + dayOfMonth);
                } else {
                    tv.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
                }
            }
        }// 設置初始日期
                , calendar.get(Calendar.YEAR)
                , calendar.get(Calendar.MONTH)
                , calendar.get(Calendar.DAY_OF_MONTH)).show();
    }
}
